package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.calendar.CalendarMultiView;
import com.hxkcloud.nurse.utils.BrightnessUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-11-28.
 * Description:日期多选
 */
public class CalendarMultiPop extends PopupWindow {

    private Activity context;
    private CalendarMultiView calendarView;
    private Button bt_cancel;
    private Button bt_sure;
    private onCalendarMultiPopFunc funcListener;

    List<Calendar> calendarss = new ArrayList<Calendar>();//选中的时间们

    public CalendarMultiPop(final Activity context, onCalendarMultiPopFunc listener) {
        this.context = context;
        this.funcListener = listener;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_calendarmulti, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_alpha);

        calendarView = (CalendarMultiView) rootView.findViewById(R.id.calendarnulti);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_calendarnulti_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_calendarnulti_sure);

        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                dismiss();
                funcListener.onCalendarMultiCancel();
            }
        });
        bt_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                dismiss();
                funcListener.onCalendarMultiSure();
            }
        });

        //设置日期点击监听器
        calendarView.setOnDateSelectedListener(new CalendarMultiView.OnDateSelectedListener() {

            @Override
            public void onDateUnselected(Date date) {
//                Calendar calendar = Calendar.getInstance();
//                calendar.setTime(date);
//                calendarss.add(calendar);
                Calendar calendarC = Calendar.getInstance();
                calendarC.setTime(date);
                for (Calendar calendarExist : calendarss) {
                    if ((calendarExist.get(Calendar.YEAR) == calendarC.get(Calendar.YEAR)) &&
                            (calendarExist.get(Calendar.MONTH) == calendarC.get(Calendar.MONTH)) &&
                            (calendarExist.get(Calendar.DAY_OF_MONTH) == calendarC.get(Calendar.DAY_OF_MONTH))) {
                        calendarss.remove(calendarExist);
                        break;
                    }
                }

            }

            @Override
            public void onDateSelected(Date date) {
                if (calendarss.size() >= 30) {
                    funcListener.onCalendarMultimore30();
                } else {
                    Calendar calendarC = Calendar.getInstance();
                    calendarC.setTime(date);
                    calendarss.add(calendarC);
                }
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });

    }

    public void setPoints(List<Calendar> cals1) {
        if (cals1 != null && cals1.size() > 0) {
            for (Calendar cal : cals1) {
                calendarView.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), true, true, cals1);
            }
        }

    }

    public void showBottom(View parent) {
        calendarView.clearOldSelections();
        calendarss.clear();
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    public List<Calendar> getCalendarss() {
        return calendarss;
    }

    public void setCalendarss(List<Calendar> calendarss) {
        this.calendarss = calendarss;
        for (Calendar calendar : calendarss) {
            calendarView.selectDate(calendar.getTime());
        }
    }

    public interface onCalendarMultiPopFunc {
        void onCalendarMultiSure();

        void onCalendarMultiCancel();

        void onCalendarMultimore30();
    }
}
